package com.ruitu.health_2.ui.common;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.BookModel;
import com.ruitu.health_2.mvp.presenter.BookPresenter;
import com.ruitu.health_2.ui.activity.BookDetailActivity;
import com.ruitu.health_2.ui.adapter.ModelBookListAdpter;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.product.Book;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

// 书籍列表
public class BookListActivity<P, M> extends BaseRefreshActivity<BookPresenter.BookListP, BookModel.BookListM> {
    protected RelativeLayout rl_empty;

    protected List<Book> bookList = new ArrayList<>();
    protected ModelBookListAdpter adapter;

    protected int currPage = 1;
    protected int totalPage = 1;
    protected String name = "";// 传过来的搜索关键字

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_book_list;
    }

    @Override
    protected void onItemClick(View v, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("name", bookList.get(position).getBookName());
        bundle.putString("content", bookList.get(position).getContentmb());
        startActivity(BookDetailActivity.class, bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rl_empty = findViewById(R.id.rl_empty);

        ((TextView) (rl_empty.findViewById(R.id.txt_description))).setText("暂时没有找到相关文章");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            name = bundle.getString("name");
        }

        setHeadTitle("书籍列表");

        reqDataList();
        showProgress();
    }

    protected void showOrHideEmptyView() {
        if (isDataEmpty(bookList)) {
            rl_empty.setVisibility(View.VISIBLE);
        } else {
            rl_empty.setVisibility(View.GONE);
        }
    }

    protected void reqDataList() {
        p.reqBookList(20, currPage);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {
            if (currPage == 1) {
                bookList.clear();
            }
            PageModel2<Book> bookPage = (PageModel2<Book>) data;
            totalPage = bookPage.getTotalPage();
            List<Book> newData = bookPage.getList();
            bookList.addAll(newData);
            adapter.setDataList(bookList);
        }

        showOrHideEmptyView();
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new ModelBookListAdpter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lrcv_list);
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        reqDataList();
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqDataList();
        } else {
            refreshNoMoreData();
        }
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
